<?php

namespace App\Http\Controllers\Home;


use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LoginController extends Controller 
{
	public $assign = [];
	
	public function login()
	{
		return view('home.login',$this->assign);
	}

	public function doLogin(Request $request)
	{
		$name = $request->post('name');
		$pwd  = $request->post('pwd');
		$pd = md6($pwd);
		$model_users = new \App\Model\Admins();
		$info = $model_users->getUserByNmae($name);
		if (!empty($info)) {
			if($pd == $info->pwd){

				$key = 'login_homeuser';
				session([$key => $info]);

				echo "<script>alert('ok');window.location.href='/';</script>";
			}else{
				echo "<script>alert('密码错误');window.location.href='/login';</script>";
			}
		} else {
			echo "<script>alert('账号或密码错误');window.location.href='/login';</script>";
		}
		

	}

	public function index()
	{	
		
		// if(null == session()->get('login_user'))
		// {
		// 	echo "<script>alert('请登录');window.location.href='/login';</script>";
		// }
		// if(!isset($key) || empty($key)){
		// 	echo "<script>alert('请登录');window.location.href='/';</script>";
		// }
		$user = session()->get('login_homeuser');
		$id = $user->id;
		$user_name = $user->name;
		$this->assign['id'] = $id;
		$this->assign['user_name'] = $user_name;

		$model_news = new \App\Model\News();
		$info = $model_news->getNews();
		$this ->assign['info'] = $info; 
		return view('home.index',$this->assign);
	}

	public function outLogin(Request $request)
	{
		$key = 'login_homeuser';
		session()->forget($key);
		// $request->seesion()->pull($key, 'default');
		return redirect('/');

	}

	
   
}





